//
// Created by ysy on 2024/5/31.
//
#include "NanoNetShfeMdApi.h"
#include "DataEngine.h"

class CNanoNetShfeMdSpiImp : public CNanoNetShfeMdSpi {

public:
    void OnNanoShfeMd(const NanoShfeMdType &refNanoShfeMd) override {
        auto it = p_engine->con_strategy_maps.find(refNanoShfeMd.inst_id);
        if (it != p_engine->con_strategy_maps.end()) {
            event.type = TICK;
            event.tick.initData(refNanoShfeMd);
            p_engine->addEvent(event);
        }

    }

    CNanoNetShfeMdSpiImp() = default;

    void addEngine(DataEngine *p_engine) {
        this->p_engine = p_engine;
    }

private:
    DataEngine *p_engine = nullptr;
    Event event;
};

class CNanoNetAdapter {
private:
    DataEngine *p_engine = nullptr;
    CNanoNetShfeMdSpiImp mdSpi = {};
    bool isStop = false;
public:
    explicit CNanoNetAdapter(DataEngine *p_engine) {

        this->p_engine = p_engine;
        mdSpi.addEngine(p_engine);

    }

    ~CNanoNetAdapter() {
        isStop = true;
    }

    void startRecv() {

        thread p1([this]() {
            cpu_set_t mask;
            CPU_ZERO(&mask);
            CPU_SET(7, &mask);
            string lpConfig = "/root/strategy/config.ini";
            CNanoNetShfeMdApi &refMdApi = CNanoNetShfeMdApi::CreateNanoShfeMdApi();

            if (0 != refMdApi.NanoStart(mdSpi, lpConfig.c_str())) {
                printf("NanoDceFunc: NanoStart failed");
                return;
            }
            int32_t nRet = -1;
            while (-1 != (nRet = refMdApi.NanoRecv())) {
                if (!isStop) {
                    if (0 == nRet) {
                        //SPDLOG_INFO("ERROR:NanoDceFunc: none data");
                        //printf("NanoDceFunc: none data\n");
                    } else if (1 == nRet) {
                        //printf("NanoDceFunc: have data\n");
                    }

                } else {
                    CNanoNetShfeMdApi::DestroyNanoShfeMdApi(refMdApi);
                    break;
                }
            }

        });
        p1.detach();
    }

};
